<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/6/006
 * Time: 15:14
 */

namespace app\api\model;


use app\api\service\Token;
use app\api\model\OrderBuy as OrderBuyModel;
use app\api\service\Order as OrderService;
use app\api\service\OrderAfterSale as OrderAfterSaleService;
use app\lib\exception\OrderException;

class OrderBuyAfterSale extends BaseModel {

    protected $hidden = ['update_time','create_time'];

    /**
     * 订单用户
     * @return \think\model\relation\belongsTo
     */
    public function orderMember() {
        return $this->belongsTo('Member');
    }

    /**
     * 售后单对应的订单
     * @return \think\model\relation\BelongsTo
     */
    public function orderBuy() {
        return $this->belongsTo('OrderBuy');
    }

    /**
     * 售后单图片
     * @return \think\model\relation\HasMany
     */
    public function images() {
        return $this->hasMany('OrderBuyAfterSaleImg');
    }

    /**
     * 获取申请售后页面数据
     * @param $id
     * @return array
     * @throws OrderException
     */
    public static function applyAftermarketData($id) {

        $uid = Token::getCurrentUid();

        $order = OrderBuyModel::get(['id' => $id, 'member_id' => $uid]);
        if (!$order) {
            throw new OrderException();
        }

        $product = $order->orderBuyProduct;

        $data = [
            'id' => $order['id'],
            'order_sn' => $order['order_sn'],
            'product_name' => $product->name,
            'product_thumb' => $product->thumb,
            'product_price' => $product->price,
            'product_num' => $product->num,
            'product_attr' => $product->attr,
            'consignee' => $order['consignee'],
            'mobile' => $order['mobile'],
            'pay_price' => number_format($order['pay_price'], 2)
        ];
        return $data;
    }

    /**
     * 获取商城订单售后列表
     * @return \think\Paginator
     */
    public static function afterSaleList() {

        $uid = Token::getCurrentUid();

        $member_after_sale_list = self::where('member_id', '=', $uid)->order('create_time desc')->paginate(15, true);

        if ($member_after_sale_list) {
            foreach ($member_after_sale_list as &$v) {
                $v->visible(['status', 'price', 'id', 'status_text', 'orderBuy'])
                    ->orderBuy->visible(['orderBuyProduct', 'order_sn'])
                    ->orderBuyProduct->visible(['product', 'name', 'thumb', 'price', 'attr', 'num'])
                    ->product->visible(['brand'])
                    ->brand->visible(['name', 'logo']);

                $v->status_text = OrderService::get_order_after_sale_status($v->status);
            }
        }

        return $member_after_sale_list;
    }

    /**
     * 获取商城售后单详情
     * @param $id
     * @return static
     */
    public static function afterSaleInfo($id) {
        $uid = Token::getCurrentUid();

        $order_after_sale = self::get(['id' => $id, 'member_id' => $uid], ['orderBuy.orderBuyProduct', 'images']);

        $order_after_sale = OrderService::orderAfterSaleVisible($order_after_sale);

        return $order_after_sale;
    }

    /**
     * 用户确认收货
     * @param $id
     * @param $member_express_sn
     * @return bool
     * @throws OrderException
     */
    public static function saveLogistics($id, $member_express_sn) {
        $uid = Token::getCurrentUid();
        $after_sale = self::get(['id' => $id, 'member_id' => $uid]);

        OrderAfterSaleService::saveLogistics($after_sale, $member_express_sn);

        return true;
    }

    /**
     * 售后单确认收货
     * @param $id
     * @return bool
     * @throws OrderException
     */
    public static function afterSaleConfirmReceipt($id) {
        $uid = Token::getCurrentUid();

        $after_sale = self::get(['id' => $id, 'member_id' => $uid]);

        OrderAfterSaleService::afterSaleConfirmReceipt($after_sale, 'orderBuy');
        return true;
    }

    /**
     * 取消售后单
     * @param $id
     * @return bool
     */
    public static function cancelAfterSale($id) {
        $uid = Token::getCurrentUid();

        $after_sale = self::get(['id' => $id, 'member_id' => $uid]);

        OrderAfterSaleService::cancelAfterSale($after_sale, 'orderItem');
        return true;
    }
}